package com.lxc.util;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class saveImageToFileSystem {
    public static String save(MultipartFile file) throws IOException {
        String uploadDir = "uploads/";

        // 创建上传目录（如果不存在）
        File dir = new File(uploadDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        String fileName = System.currentTimeMillis() + "_" + file.getOriginalFilename();
        Path path = Paths.get(uploadDir + fileName);

        // 真正将文件写入磁盘
        Files.write(path, file.getBytes());

        return "/uploads/" + fileName; // 返回访问路径
    }
}
